import sys
import os
import PySimpleGUI as sg
import time
#dir_path=sg.popup_get_inr('Select Folder')
#print(dir_path)
#原理：检查指定位置的文件夹， 后缀为C00，C01，C02开头的，到Location_C目录下查看是否存在
#存在的话，标识为绿色，不存在的话，标识为红色

#把windows路径转为python可以接受的linux路径
def linux_location(location):
    return location.replace('\\','/')

#原目录
#location_source='\\10.102.200.9\DataHandling\UPP\SIMdatabackup\45474780-10'
#location_source=input('lolcation_source:')
#location_source=dir_path
#location_source=linux_location(location_source)
#后缀为C的目标核对位置
location_C='//10.102.200.5/TestDataFile/CD'
location_C=linux_location(location_C)
#后缀为P的目标核对位置
location_P='//10.102.200.5/Prodata/Production'
location_P=linux_location(location_P)                        

layout = [    [sg.Input(key='-FOLDER-'), sg.FolderBrowse(key='-FOLDER-'),sg.Button('确定')],

    [sg.MLine(key='-ML1-'+sg.WRITE_ONLY_KEY, size=(40,50))],]


window = sg.Window('结果校验', layout, finalize=True)
# Note, need to finalize the window above if want to do these prior to calling window.read()
#window['-ML1-'+sg.WRITE_ONLY_KEY].print(time.time(),end='', text_color='red', background_color='yellow')
#window['-ML1-'+sg.WRITE_ONLY_KEY].print('\n', end='')
#window['-ML1-'+sg.WRITE_ONLY_KEY].print(1,2,3,4,text_color='white', background_color='green')

#打开颜色标识
#color = sys.stdout.shell

#检查是否存在C和P文件
def list_files(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file[-3]=="C" or file[-4]=="C":
                #print("C",file)
                check_file_exist(file,location_C)
            if file[-3]=="P" and file[-3:]!="PNG":
                #print("P",file,file[-3:])
                check_file_exist(file,location_P)
                pass
            #print(file,file[-3])



def check_file_exist(file,location):
    if file in os.listdir(location):
        #color.write(file+" existed in "+location+"\n","STRING")
        #sg.cprint(file, font='Courier 12', colors='white on green', )
        window['-ML1-'+sg.WRITE_ONLY_KEY].print(file,text_color='white', background_color='green')
        return True
    else:
        #color.write(file+" not in "+location+"\n","COMMENT")
        #sg.cprint(file, font='Courier 12', colors='white on red', )
        window['-ML1-'+sg.WRITE_ONLY_KEY].print(file,text_color='white', background_color='red')
        return False


#list_files(location_source)
while True:
    event, values = window.read()
    folder_path = values['-FOLDER-']
    #
    
    #print(folder_path)
    window['-ML1-'+sg.WRITE_ONLY_KEY].update('')
    list_files(folder_path)
    
    if event == sg.WIN_CLOSED or event == 'Exit':
        break

window.close()



print('*************End*******************')

